<template>
	<view>
		<!-- 搜索框 -->
		<uni-list>
			<uni-search-bar  @blur="getSchoolByname" @clear="getSchoolByname({value:''})" class="uni-mt-10" radius="5" placeholder="输入学校名称" clearButton="auto" cancelButton="none"  />
		</uni-list>
		<!-- 当前地址 -->
		<uni-section :title="deptData.deptName" titleFontSize="18px">
			<template v-slot:decoration>
				<view>
					<uni-icons type="paperplane-filled"  size="20"></uni-icons>
				</view>
			</template>
			<template v-slot:right>
				<view style="font-size: small;">
					当前位置
				</view>
			</template>
		</uni-section>
		<!-- 附近地址 -->
		<uni-section title="附近地址" type="line" class="addressList">
			<template v-slot:decoration>
				<view>
					<uni-icons type="map-pin-ellipse" color="#0278FF" size="20"></uni-icons>
				</view>
			</template>
			<uni-list>
				<uni-list-item v-for="item,index in deptList" :key="item.deptId" :title="item.deptName" clickable	 @click="chooseAddress(item)" /> 
			</uni-list>
		</uni-section>
	</view>
</template>

<script>
	import { getAddress } from "@/api/print/address.js";
	
	export default {
		data() {
			return {
				longitude: null,
				latitude: null,
				deptData: this.$store.state.system.location,
				DeptName: null,
				deptId: null,
				deptList: [{name:"湖北民族大学"},{name:"湖北民族学院"}]
			}
		},
		onReady(){
			console.log("@@@@");  
			console.log('this.deptData',this.deptData );
			this.getSchool()			

		},
		methods: {
			// 获取学校
			getSchool(){ 
				let that = this
				console.log("afd")
			// #ifdef MP-WEIXIN
				wx.getLocation({
				 type: 'wgs84',
				async success  (res) {
				console.log(res);
				  that.latitude = res.latitude
				  that.longitude = res.longitude 
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);										
					uni.showLoading({title: '加载中'});
					const data = await getAddress(res.longitude,res.latitude);
					uni.hideLoading();
					console.log(data);
					if(data.code!=200) {
					return uni.showToast({
							title: '查询失败，稍后再试',
							duration: 2000
						});
					} 
					that.deptList = data.rows
				 },
					fail(res) {
						console.log(res);
					},
					
				}) 
				// #endif
				// #ifndef MP-WEIXIN
				uni.getLocation({
					type: 'wgs84',
					async success (res) {
					that.latitude = res.latitude
					that.longitude = res.longitude 
						console.log('当前位置的经度2：' + res.longitude);
						console.log('当前位置的纬度2：' + res.latitude);						
						uni.showLoading({title: '加载中'});
						const data = await getAddress(res.longitude,res.latitude,this.DeptName);
						uni.hideLoading();
						if(data.code!=200) {
							return uni.showToast({
									title: '查询失败，稍后再试',
									duration: 2000
								});
						} 
						that.deptList = data.rows
						console.log(that.deptList);
					}
				});
				// #endif 
			},
			//根据名字获取学校列表
			async getSchoolByname(e){
				console.log(e.value);
				uni.showLoading({title: '加载中'});
				const data = await getAddress(this.longitude,this.latitude,e.value);
				uni.hideLoading();
				console.log(data);
				if(data.code!=200) {
				return uni.showToast({
						title: '查询失败，稍后再试',
						duration: 2000
					});
				} 
				this.deptList = data.rows
			},			
			//选择学校
			chooseAddress(item){
				console.log("选择学校地址");
				this.$store.commit('SET_LOCATION',item)
				// uni.setStorageSync('location',item)
				this.deptData = item
				// 在C页面内 navigateBack，将返回A页面
				uni.navigateBack({
					delta: 1
				});
				this.refreshData();

				// uni.redirectTo({
				// 	url: '/pages/index'
				// });
				console.log('this.deptData',this.deptData );
			}
		}
	}
</script>

<style>
  .decoration{
    width: 6px;
    height: 6px;
		margin-right: 4px;
    border-radius: 50%;
		background-color: $uni-success;
  }
	/* 附近地址 */
	.addressList{
		margin-top: 20rpx;
	}
</style>
